#!/usr/bin/env python
#encoding=utf-8


# patch object
# get from mail object
import re
from config import config
from base import base
import util
from exception import InValidError

class mypatch(base):
	
	def __init__(self, message):
		super(mypatch, self).__init__()
		self._message = message
		self._message_subject = ''
		self._body = ''
		self._valid = False
		self._build = ''
		self._name = ''
		self._from = ''
		self._date = ''
		self._path = ''
		self._applied = False

		# some comments
		self._comment = 'no comments'
		# to do
		self._branch = 'powerkvm'
		
		self.parse_message()

	@property
	def comment(self):
		return self._comment
	
	@comment.setter
	def comment(self, new_value):
		self._comment = new_value

	@property
	def applied(self):
		return self._applied

	@applied.setter
	def applied(self, new_value):
		self._applied = new_value

	@property
	def path(self):
		return self._path

	@path.setter
	def path(self, new_value):
		self._path = new_value

	@property
	def body(self):
		return self._body

	@property
	def git_name(self):
		return self._name;

	@property
	def branch(self):
		return self._branch

	@property
	def valid(self):
		return self._valid
	
	@property
	def subject(self):
		return self._message_subject

	@property
	def from_who(self):
		return self._from

	@property
	def date(self):
		return self._date

	@property
	def name(self):
		return self._name

	def parse_message(self):
		self._message_subject = \
                         util.trim(self._message['subject'].lower())
		self._from = self._message['From']
		self._date = self._message['Date']
		#self._body = self._message.get_payload(decode=True)
		if not self._body:
			self._body = self._message.as_string()
		try:
			(self._build, self._name) = \
			util.get_branch_gitname(self._message_subject)
			if self._name[0:5] == 'linux' or self._name == 'kernel':
				self._name = 'linux'
		except InValidError:
			self._valid = False
		else:
			self._valid = \
                                      True if self._name \
                                      in self._conf.get('git').split(',') \
                                      else False

		if util.is_cover_letter(self._message_subject):
			self._valid = False

	def debug(self):
		print "self._message_subject %s" % self._message_subject
		a = 'valid' if self._valid == True else 'invalid'
		print a
		a = 'applied' if self._applied == True else 'not applied'
		print a
		print "from [%s]" % self._from
		print "date [%s]" % self._date
		print "_path [%s]" % self._path
		print "branch [%s]" % self._branch
		print "comment [%s]" % self._comment
		
if __name__ == "__main__":
	msg = "[Frobisher xxx Qemu] sdfddf\r\n jsldfjsdf"
	p = patch(msg)
	p.valid
	p.name = 'hi'
	p.debug()
